<?php
require_once("Validator.php");
require_once("ValidationError.php");
/**
 * This class rapresents a validation set, stores all Validators needed by a 
 * form. It is just a Facade to provide a more comfortable way to deal with the
 * "library". 
 *
 */
class ValidationSet {
	
	private $validators;
	private $errors;
	
	public function __construct() {
		
		$this->validators 	= array();
		$this->errors		= array();
	}
	
	public function addValidator(Validator $v) {
		array_push($this->validators, $v); 
	}
	
	public function performValidation() {
		
		$isValid = true;
		
		foreach ($this->validators as $validator) {
			
			$result = $validator->validate();
			if ( ($result!=null) && ($result instanceof ValidationError) ) {
				$this->errors[$result->getFieldName()] = $result;
				$isValid = false;
			}
		}
		
		return $isValid;
		
	}
	
	public function getErrors() {
		return $this->errors;
	}
	
	public function getErrorByFieldname($fieldName) {

		return (array_key_exists($fieldName, $this->errors)) 
			   ? $this->errors[$fieldName]
			   : null;
	}	
	
}
?>